<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 CalenderUI();
 link_js(array('jquery.fixedheader','bootstrap-typeahead'),'asset/js/');
 ?>
<!--<div class='container'>-->
     <div class="tabbable"> <!-- Only required for left/right tabs -->
	<?=$tab_header;?>
       <div class="tab-content" style=''>
	   <br>
            <div class="tab-pane" id="table1"></div>
            <div class="tab-pane active" id="table2"><?=$this->zetro_auth->allAuth($frm3,'list_karyawan');?><?=$this->zetro_auth->allAuth($list,'list_karyawan');?></div></div>
       </div>
		<input type='hidden' id='row_aktif' value='<?=empty($_GET['ot'])?'n':$_GET['ot'];?>'/>
		<input type='hidden' id='rows' value='<?=base64_decode($this->session->userdata('menus'));?>'/>
    </div>
<!--</div>-->
<?=$modal;?><?=$modal2;?>
<script language='javascript'>
$(document).ready(function(e) {
	$('div.tab-content').css('height',(screen.availHeight-275));
	$('#bln').html("<?=$bln;?>").change(function(){_show_data('1');});
	$('#thn').html("<?=$thn;?>").change(function(){_show_data('1');});
	$('#jenis_pinjaman').html("<?=$lok;?>")
	$('#id_lokasi').html("<?=$lok2;?>").change(function(){_show_data('1');})
	$('#frm4').validationEngine();
	$('a#p-table1').live('click',function(){$('#batal').click();$('#frm4 input select').val('');_show_popup('');});
	$('#batal').click(function(){/* document.frm4.reset();*/$('#myModal').modal('hide');$('a#p-table2').click();});
	$('#simpan').click(function(){ _simpan_data();});
	$('.close').click(function(){ $('#batal').click();})
	 _get_member('R');
	 _get_member('D');
	$('#tgl_trans').datepicker();
	$('#tgl_trans').datepicker("option","dateFormat","dd/mm/yy");
	 tglNow('#tgl_trans');
	 formatAngka('jml_pinjaman');
	 formatAngka('jml_cicilan');
	 $('#no_member').live('change',function(){_get_detail($(this).val());});
	 $('#nm_member').live('change',function(){_get_detail($(this).val());});
	 _show_data('');
	 $('#c_cari').click(function(){_show_data('1');});
	 $('#cicilan').focusout(function(){ _hitung_cicilan2();})
	 $('#bunga_pinjaman')
	 	//.live('change',function(){ _hitung_ccl();});
		.focusout(function(){ _hitung_bunga2();})
})
/**
* support function
*/
function _simpan_data()
{
	$('#jenis_pinjaman').removeAttr('disabled');
	$.post("setpinjamanmember",$('#frm4').serialize(),function(result){
				_show_data('1');
				$('#jenis_pinjaman').attr('dsiabled','disabled');
				$('#batal').click();
				
	})
}
function _show_data(tp)
{
	show_indicator('NewList',9);
	$.post('getlistpinjaman',$('#frm1').serialize()+'&ot='+$('#row_aktif').val(),function(result)
	{
		$('#NewList tbody').html(result);
		(tp=='')?
		$('#NewList').fixedHeader({'width':(screen.availWidth-50),'height':(screen.availHeight-370)}):'';
	})
}

function _show_popup(id)
{
	 tglNow('#tgl_trans');
	 $('#jenis_pinjaman')	
	 	.val(($('#row_aktif').val()=='y')?'4':'5').select()
		.attr('disabled','disabled');
		$('#no_member').removeAttr('readonly');
		$('#nm_member').removeAttr('readonly');
	 $('#jenis_pinjaman').removeAttr('disabled');
	 (id=='')?'':_get_detail_pinjaman(id);
	 $('.modal-body').css('overflow','hidden');
	 $('#myModal').modal('show');	
}

function _get_member(tp)
{
	$.post('getakunmember',{'st':tp},function(result){
		(tp=='R')?$('#no_member').typeahead({source:$.parseJSON(result)}):
		$('#nm_member').typeahead({source:$.parseJSON(result)});});
}
function _get_barang()
{
	$.post('getkdbarang',{'tp':tp},function(result)
	{
		$('#nm_barang').typeahead({source:$.parseJSON(result)});
	})
}
function _hidden_row(id,st){$('div.control-group#g_'+id).css({'display':st});}
/**get detail member name or member id*/
function _get_detail(fld)
{
	$.post('get_akun_detail',{'id':fld},function(result)
	{
		var r=$.parseJSON(result);
		$('#nm_member').val(r.nm_member);
		$('#no_member').val(r.id_reg);
		_get_saldo_simpanan(r.id_reg,($('#row_aktif').val()=='n')?"saldo_pinjaman_uang":"saldo_pinjaman_barang")
		})
}
function _get_saldo_simpanan(id,fld)
{
	$.post('getsaldosimpanan',{'id_member':id,'fld':fld},
	function(result)
	{
		$('#saldo_pinjaman').attr('readonly','readonly').val(result).priceFormat();
	})
}
function _get_detail_pinjaman(id)
{
	$.post('getdetailpinjaman',{'id':id},function(result)
	{
		var r=$.parseJSON(result)
		$('#id').val(r.id);
		$('#tgl_trans').val(tglFromSql(r.tgl_trans));
		$('#no_member').val(r.id_member).attr('readonly','readonly');	
		$('#nm_member').val(r.nm_member).attr('readonly','readonly');	
		$('#saldo_pinjaman').val(r.saldo_pinjaman).attr('readonly','readonly').priceFormat();	
		$('#jenis_pinjaman').val(r.jenis_pinjaman).select().removeAttr('disabled','disabled');	
		$('#jml_pinjaman').val(r.jml_pinjaman).priceFormat();	
		$('#cicilan').val(r.lama_cicilan);	
		$('#bunga_pinjaman').val(r.bunga_pinjaman);	
		$('#jml_cicilan').val(r.jml_cicilan).priceFormat();	
		$('#cara_bayar').val(r.cara_bayar).select();	
		$('#ket_pinjaman').val(r.ket_trans);
	})
}
function btn_click(id,aksi)
{
	switch(aksi)
	{
		case 'process':
		break;
		case 'show':
		_show_popup_(id);
		break;
		case 'update':
		_show_popup(id);
		break;
		case 'deleted':
		jConfirm('Yakin pinjaman ini akan dibatalkan?','Ziebar Alert',function(r)
		{
			if(r)
			{
				$.post('deletepinbar',{'id':id},function(rst){ _show_data('1');})
			}
		})
		break;
	}
}

function _hitung_cicilan2()
{
	var r=$('#jml_pinjaman').val().replace(/,/g,'');
	var l=$('#cicilan').val();
	var tl=parseInt(r)/parseInt(l);
	$('#jml_cicilan').val(Math.round(tl)).priceFormat();
}

function _hitung_bunga2()
{
	var r=$('#jml_pinjaman').val().replace(/,/g,'');
	var b=$('#bunga_pinjaman').val().replace('%','');
	$('#jml_cicilan_bunga').val(Math.round((r*(b/100)/12))).priceFormat();
}

function _show_popup_(id)
{
	$('#simpan2').attr('disabled','disabled');
	$.post('realisasi_approve',{'id':id},function(result){
	 $('.modal-body').css('overflow','hidden').html(result);
	 $('#myModal2').modal('show');	
	})
}

</script>

